package org.elsfs.cloud.common.mybatis.ext.sql.support.condition;


import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.common.mybatis.ext.sql.SqlParameterManager;
import org.elsfs.cloud.common.util.lang.NamingCase;

/**
 * 区间结束值条件
 *
 */
@RequiredArgsConstructor
public class BetweenEndValueConditionItem implements ConditionItem {

    /**
     * SQL参数管理器
     */
    protected final SqlParameterManager sqlParameterManager;
    /**
     * 左边字段名称
     */
    protected final String leftField;
    /**
     * 区间开始字段名称
     */
    protected final String beginField;
    /**
     * 区间结束值
     */
    protected final Object endValue;

    /**
     * 构建SQL片段
     *
     * @return SQL片段
     */
    @Override
    public String buildSqlFragment() {
        return String.format(
            "%s BETWEEN %s AND %s",
            NamingCase.toUnderlineCase(this.leftField),
            NamingCase.toUnderlineCase(this.beginField),
            SqlParameterManager.buildParameterPlaceholder(
                this.sqlParameterManager.registerParameter(this.endValue)
            )
        );
    }
}
